package com.foreveross.project.projectonline.config;

import java.lang.reflect.Field;

import org.iff.infra.util.FCS;
import org.iff.infra.util.Logger;
import org.iff.infra.util.MapHelper;

import com.foreveross.project.projectonline.jfinal.plugin.JedisSentinelCache;
import com.foreveross.project.projectonline.jfinal.plugin.ResourceKit;
import com.foreveross.project.projectonline.jfinal.plugin.SslJettyServer;
import com.foreveross.project.projectonline.model.*;
import com.foreveross.project.projectonline.web.controller.*;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.spring.SpringPlugin;
/**
 * API引导式配置 
 */
public class AppConfig extends JFinalConfig {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		String deployMode = System.getProperty("deploy.mode", "dev");
		String fileName = "dev_config.properties";
		if ("product".equals(deployMode)) {
			fileName = "product_config.properties";
		} else if ("test".equals(deployMode)) {
			fileName = "test_config.properties";
			me.setDevMode(true);
		} else {
			fileName = "dev_config.properties";
			me.setDevMode(true);
		}
		Logger.debug(FCS.get("[deployMode:{0},fileName:{1}]", deployMode, fileName));
		loadPropertyFile(fileName);
		ResourceKit.setDefaultProperties(fileName);
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", HomeController.class);
		me.add("/login", LoginController.class);
		me.add("/captcha", CaptchaController.class);
		me.add("/userVerify", UserVerifyController.class);
		me.add("/userAdmin", UserAdminController.class);
		me.add("/project", ProjectController.class);
		me.add("/createProject", CreateProjectController.class);
		me.add("/myInfo", MyInfoController.class);
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件

		String jdbcUrl = System.getProperty("jdbcUrl", getProperty("jdbcUrl"));
		String user = System.getProperty("user", getProperty("user"));
		String password = System.getProperty("password", getProperty("password"));
		Logger.debug(FCS.get("url:{0}\n,username:{1}\n,password:{2}", jdbcUrl, user, password));
		C3p0Plugin c3p0Plugin = new C3p0Plugin(jdbcUrl, user, password);
		me.add(c3p0Plugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		{
			arp.setCache(new JedisSentinelCache());
			// public tables
			arp.addMapping("TP_MGT_USER", "ID", TpMgtUser.class);
			arp.addMapping("TP_MGT_ROLE", "ID", TpMgtRole.class);
			arp.addMapping("TP_MGT_USERROLE", "ID", TpMgtUserRole.class);
			arp.addMapping("TD_OL_PROJECT","ID", Project.class);
		}
		me.add(arp);

		//添加srping插件
		String springConfig = System.getProperty("springconfig");
		if (springConfig != null && springConfig.length() > 1) {
			SpringPlugin springPlugin = new SpringPlugin(springConfig);
			me.add(springPlugin);
		} else {
			SpringPlugin springPlugin = new SpringPlugin();
			me.add(springPlugin);
		}
		{//add Redis plugin
			//JedisSentinelPlugin jedis = new JedisSentinelPlugin();
			//CacheKit.setKit("Jedis");
			//me.add(jedis);
		}
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new CommonInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new CommonHandler(MapHelper.toMap("ICON_PATH", ResourceKit.getValue("imageUrlPre"), "DOWNLOAD_PATH",
				ResourceKit.getValue("downloadUrlPre"))));
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		System.out.println(System.getProperties());
		String scanIntervalSeconds = System.getProperty("scanIntervalSeconds", "5");
		String context = System.getProperty("context", "/mgt");
		String port = System.getProperty("port", "9999");
		String webdir = System.getProperty("webdir", "src/main/webapp");
		int scanDefault = 5;
		int portDefault = 9999;
		try {
			scanDefault = Integer.valueOf(scanIntervalSeconds);
		} catch (Exception e) {
		}
		try {
			portDefault = Integer.valueOf(port);
		} catch (Exception e) {
		}
		Logger.debug(FCS.get("scanIntervalSeconds:{0},context:{1},port:{2}", scanDefault, context, portDefault));
		{
			SslJettyServer sever = new SslJettyServer(webdir, portDefault, context, scanDefault);
			//JFinal.start(webdir, portDefault, context, scanDefault);
			try {
				Field field = JFinal.class.getDeclaredField("server");
				field.setAccessible(true);
				field.set(null, sever);
				sever.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
